
package com.ifcar.app.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;

public class VerticalScrollTabs extends ScrollView {
    private View mTopView;

    private View mBottomView;

    public VerticalScrollTabs(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public VerticalScrollTabs(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VerticalScrollTabs(Context context) {
        super(context);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        checkScrollY();
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        checkScrollY();
    }

    private void checkScrollY() {
        View child = getChildAt(0);
        if (child == null) {
            setViewVisibility(mTopView, View.INVISIBLE);
            setViewVisibility(mBottomView, View.INVISIBLE);
            return;
        }

        int scrollY = getScrollY();
        if (scrollY <= 0) {
            setViewVisibility(mTopView, View.INVISIBLE);
        } else {
            setViewVisibility(mTopView, View.VISIBLE);
        }

        int maxScrollY = child.getBottom() + getPaddingBottom() - getHeight();
        if (scrollY >= maxScrollY) {
            setViewVisibility(mBottomView, View.INVISIBLE);
        } else {
            setViewVisibility(mBottomView, View.VISIBLE);
        }
    }

    private void setViewVisibility(View v, int visibility) {
        if (v != null) {
            v.setVisibility(visibility);
        }
    }

    public void setTopView(View top) {
        mTopView = top;
    }

    public void setBottomView(View bottom) {
        mBottomView = bottom;
    }

    public void scrollToTop() {
        scrollTo(0, 0);
    }

    public void scrollToBottom() {
        View child = getChildAt(0);
        if (child == null) {
            return;
        }
        int maxScrollY = child.getBottom() + getPaddingBottom() - getHeight();
        scrollTo(0, maxScrollY);
    }
    
    public void scrollToPosition(int position) {
        ViewGroup child = (ViewGroup)getChildAt(0);
        if (child == null) {
            return;
        }
        if (position < 0 || position >= child.getChildCount()) {
            return;
        } if (position == 0) {
            scrollToTop();
        } else if (position == child.getChildCount() - 1) {
            scrollToBottom();
        } else {
            View item = child.getChildAt(position);
            int top = Math.min(item.getTop(), child.getBottom() - getHeight());
            scrollTo(0, top);
        }
    }
}
